<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\shop\controller;

use app\admin\model\GoodsModel;
use app\shop\model\CarModel;
use app\store\model\StoreModel;
use cmf\controller\HomeBaseController;

class GoodController extends HomeBaseController
{
    //商品详情页
    public function index()
    {
        $param=$this->request->param();
        $id=isset($param['id'])?$param['id']:0;
        if($id){
            $goods=new GoodsModel();
            $good=$goods->where('id', $id)->find();

            if (cmf_is_user_login()) {
                $user_id=cmf_get_current_user_id();
                $car=new CarModel();
                $num=$car->count_mum($user_id);
            }else{
                $num=0;
            }
            $this->assign('num',$num);
            $this->assign('good',$good);
            return $this->fetch(':goodinfo');
        }else{
            $this->error('商品不存在');
        }
    }
    //购物车页
    public function car(){
        if (cmf_is_user_login()) { //已经登录时直接跳到首页
            $user_id=cmf_get_current_user_id();
            $car=new CarModel();
            $car_list=$car->get_car_list($user_id);
            $this->assign('car_list',$car_list);
            return $this->fetch(':car');
        } else {
            return $this->fetch("../user/login");
        }
    }
    //添加购物车
    public function add_car(){
        $param=$this->request->param();
        if (cmf_is_user_login()) { //已经登录时直接跳到首页
            $good_id=$param['good_id'];
            $user_id=cmf_get_current_user_id();
            $car=new CarModel();
            $car->edit_car($good_id,$user_id,1);
            $data['num']=$car->count_mum($user_id);
            $this->success('成功加入购物车！',NULL,$data);
        } else {
            $this->error("您还未登录！");
        }
    }
    //编辑购物车
    public function edit_car(){
        $param=$this->request->param();
        if (cmf_is_user_login()) { //已经登录时直接跳到首页
            $good_id=$param['good_id'];
            $type=$param['type'];
            $user_id=cmf_get_current_user_id();
            $car=new CarModel();
            $car->edit_car($good_id,$user_id,$type);
            $data['num']=$car->count_mum($user_id,$good_id);

            $count_post_price=0;
            $count_post_score=0;
            $car_list=$car->get_car_list($user_id);
            foreach ($car_list as $key=>$val){
                if($val['is_checked']==1){
                    $count_post_price=$count_post_price+$val['goods']['post_price']*$val['num'];
                    $count_post_score=$count_post_score+$val['goods']['post_score']*$val['num'];
                }

            }
            $data['count_price']="￥".$count_post_price."+".$count_post_score."积分";
            $this->success('编辑成功！',NULL,$data);
        } else {
            $this->error("您还未登录！");
        }
    }

    public function edit_select(){
        $param=$this->request->param();
        if (cmf_is_user_login()) { //已经登录时直接跳到首页
            $type=$param['type'];
            $user_id=cmf_get_current_user_id();
            $good_id=isset($param['good_id'])?$param['good_id']:0;
            $car=new CarModel();
            $car->edit_select($good_id,$type,$user_id);
            $count_post_price=0;
            $count_post_score=0;
            $car_list=$car->get_car_list($user_id);
            foreach ($car_list as $key=>$val){
                if($val['is_checked']==1){
                    $count_post_price=$count_post_price+$val['goods']['post_price']*$val['num'];
                    $count_post_score=$count_post_score+$val['goods']['post_score']*$val['num'];
                }

            }
            if($count_post_price==0&&$count_post_score==0){
                $data['num']=0;
            }else{
                $data['num']=1;
            }
            $data['count_price']="￥".$count_post_price."+".$count_post_score."积分";
            $this->success('编辑成功！',NULL,$data);
        } else {
            $this->error("您还未登录！");
        }
    }

    public function del_car(){
        $param=$this->request->param();
        if (cmf_is_user_login()) { //已经登录时直接跳到首页
            $user_id=cmf_get_current_user_id();
            $good_id=isset($param['good_id'])?$param['good_id']:0;
            $car=new CarModel();
            $car->del_car($good_id,$user_id);
            $count_post_price=0;
            $count_post_score=0;
            $car_list=$car->get_car_list($user_id);
            foreach ($car_list as $key=>$val){
                if($val['is_checked']==1){
                    $count_post_price=$count_post_price+$val['goods']['post_price']*$val['num'];
                    $count_post_score=$count_post_score+$val['goods']['post_score']*$val['num'];
                }

            }
            $data['count_price']="￥".$count_post_price."+".$count_post_score."积分";
            $this->success('删除成功！',NULL,$data);
        } else {
            $this->error("您还未登录！");
        }
    }
}
